package cn.shlbysj.demo.service.impl;

import cn.shlbysj.demo.common.ConstansUtil;
import cn.shlbysj.demo.common.PageResult;
import cn.shlbysj.demo.common.Result;
import cn.shlbysj.demo.common.UuidUtil;
import cn.shlbysj.demo.entity.ArtInfo;
import cn.shlbysj.demo.entity.ArtInfoExample;
import cn.shlbysj.demo.entity.DownloadInfo;
import cn.shlbysj.demo.entity.DownloadInfoExample;
import cn.shlbysj.demo.mapper.ArtInfoMapper;
import cn.shlbysj.demo.mapper.DownloadInfoMapper;
import cn.shlbysj.demo.service.IDownloadInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service("downloadInfoService")
@Transactional
public class IDownloadInfoServiceImpl implements IDownloadInfoService{
    @Autowired
    private DownloadInfoMapper downloadInfoMapper;

    @Override
    public PageResult findPage(String title, int page, int rows) {
        //设置分页查询条件
        PageHelper.startPage(page,rows);
        DownloadInfoExample example=new DownloadInfoExample();
        example.setOrderByClause("`create_time` desc");
        DownloadInfoExample.Criteria criteria = example.createCriteria();
        if(title!=null && !"".equals(title)){
            criteria.andTitleLike(title);
        }
        Page<DownloadInfo> pageList = (Page<DownloadInfo>) downloadInfoMapper.selectByExample(example);
        return new PageResult(pageList.getTotal(),pageList.getResult());
    }

    @Override
    public Result insert(DownloadInfo downloadInfo) {
        downloadInfo.setdId(UuidUtil.getUUID());
        downloadInfo.setCreateTime(new Date());
        downloadInfo.setdCount(UuidUtil.getRand(200));

        int ret= downloadInfoMapper.insert(downloadInfo);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }

    @Override
    public Result delete(String dId) {
        int ret= downloadInfoMapper.deleteByPrimaryKey(dId);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }
}
